<?
$picMaxSize = "298*172";
$picMinSize = "529*594";
$action = new Action();
class Action
{
	function Action()
	{
		global $gAct,$gOp,$gSmarty;
		$gSmarty->assign("act",$gAct);
		switch($gOp)
		{
            case "ajaxQuery":
			    $this->ajaxQuery();
			    break;
			case "detail":
			    $this->detail();
				break;
			case "topdf":
			    $this->topdf();
			    break;
			case "view":
			    $this->view();
			    break;
			case "checkFileExist":
			    $this->checkFileExist();
			    break;
			case "mainAll":
				$this->mainAll();
				break;
			default:
				$this->index();
				break;
		}
	}
	function mainAll(){
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION;
		$gSmarty->assign("username",$_SESSION['adminUser']['name']);
		$gSmarty->assign("password",$INCOME['p']);
		$gSmarty->assign("r",$_SESSION['adminUser']['mayLook']);
		$gSmarty->display(basename(__FILE__,'.php')."/mainAll.tpl");
	}
	function checkFileExist()
	{
		global $INCOME;
		$f = $INCOME['f'];
		if(file_exists($f))
		{
			if(filesize($f)>1)
			{
				echo "1";
			}
			else
			{
				echo "0";
			}
		}
		else
		{
			echo "0";
		}
	}
	function view()
	{
    	global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$gCurTime,$gFileType;
    	if($INCOME['id'])
    	{
			$rf = $DB->db_query_first("select path,ext from res_file where id=".$INCOME['id']);
    		$file = $rf['path'];
    		$baseFileName = basename($file);
			$webPath = str_replace(basename(__FILE__),"",__FILE__);
			$webPath = str_replace("actions/","",$webPath);
            if($rf['ext']!="pdf")
            {
            	$tempPdf = "res/temp/".substr($baseFileName,0,strrpos($baseFileName,".")).".pdf";
            }
			else
			{
				$tempPdf = $rf['path'];
			}
			$gSmarty->assign("f",$tempPdf);
			$gSmarty->display(basename(__FILE__,'.php')."/".__FUNCTION__.".tpl");
    	}
    	
	}
    function topdf()
    {
    	global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$gCurTime,$gFileType;
    	$file = $INCOME['fid'];
    	$rf = $DB->db_query_first("select path,ext from res_file where id=".$file);
    	$file = $rf['path'];
		if(file_exists($file))
		{
			if($gFileType[$rf['ext']]['id']==40)
			{
				 if($rf['ext']!="pdf")
				 {
					$baseFileName = basename($file);
					$webPath = str_replace(basename(__FILE__),"",__FILE__);
					$webPath = str_replace("actions/","",$webPath);
					$src = $webPath.$file;
					$tempPdf = $webPath."res/temp/".substr($baseFileName,0,strrpos($baseFileName,".")).".pdf";
					$logFile = $webPath."res/pdf.log";
					//echo $src."<br/>";
					//echo "python /data/program/office/DocumentConverter.py $src $tempPdf  >> $logFile &";
					if(!file_exists($tempPdf))
					{
						//echo "python /data/program/office/DocumentConverter.py $src $tempPdf  >> $logFile &";
						$myfile = fopen($tempPdf, "w");
						fclose($myfile);
						system("python program/office/DocumentConverter.py $src $tempPdf  >> $logFile &");
					}
				 }
			}
		}
    }
	function detail()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$gCurTime,$gFileType;
		 $rf = $DB->db_query_first("select * from res_file where id=".$INCOME['id']);
		//$rf['size'] = $this->sizecount($rf['size']);
		//$rf['path'] = str_replace("s.",".",$rf['path']);
		//$gSmarty->assign("f",$rf);
		$isShowEdit = "0";
		if($_SESSION['adminUser']['roleId']!=3)
		{
			if($_SESSION['adminUser']['roleId']==2)
			{
			  	if($rf['uid']==$_SESSION['adminUser']['id']) //是我上传的图片
			  	{
			  		$isShowEdit = "1";
			  	}
			}
			else //管理员 or 审批者
			{
				$isShowEdit = "1";
			}
		}
		$isShowPreview = "0";
 
		if($gFileType[$rf['ext']]['id']==40)
		{
			$isShowPreview = "1";
		}
		$isShowDown = "0";
		if($rf['canDown']=="1")
		{
			$isShowDown = "1";
		}
		$gSmarty->assign("isShowDown",$isShowDown);
		$gSmarty->assign("isShowPreview",$isShowPreview);
		$gSmarty->assign("isShowEdit",$isShowEdit);
		$gSmarty->assign("id",$INCOME['id']);
		$gSmarty->assign("roleId",$_SESSION['adminUser']['roleId']);
		$gSmarty->display(basename(__FILE__,'.php')."/".__FUNCTION__.".tpl");
	}
    function ajaxQuery()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$GJson;
		$data =$this->queryData();
		echo $GJson->encode(array("st"=>"ok","data"=>$data));
	}
	function index()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION;
		if($_SESSION['adminUser']['roleId']==3)
		{
			$f = "f";
		}
			$sql = "SELECT * FROM res_ref order by id";
			$DB->db_query($sql);
			$data = null;
			$idx = 0+($page-1)*$iPerPage;
			while($r = $DB->db_fetch_row())
			{
		    $idx++;
            if(empty($r['pic']))
			{
				$r['pic'] = 'res/de.png';
			}
		    $data[] = $r;	
		if(empty($data))
		{
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
		}
		$gSmarty->assign("data",$data);
		}
		$gSmarty->assign("roleId",$_SESSION['adminUser']['roleId']);
		
		$gSmarty->display(basename(__FILE__,'.php')."/".__FUNCTION__.$f.".tpl");
	}
	function queryData()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION;
		if($INCOME['b'])
		{
			$conArr[] ="brandId = '".$INCOME['b']."'";
		}
		if($INCOME['c'])
		{
			$conArr[] ="carId = '".$INCOME['c']."'";
		}
		if($INCOME['t'])
		{
			$conArr[] ="typeId = '".$INCOME['t']."'";
		}
		if($INCOME['s'])
		{
			$conArr[] ="styleId = '".$INCOME['s']."'";
		}
		if($INCOME['y'])
		{
			$conArr[] ="year = '".$INCOME['y']."'";
		}
		if($INCOME['k'])
		{
			if($INCOME['k']==-1)
			{
				$conArr[] ="isPost = 0";
			}
			else if($INCOME['k']==1)
			{
				$conArr[] ="isPost = 1";
			}
		}
		if($conArr)
		{
			$con ="where ". implode(" and ",$conArr);
		}
 
 		$sql = "SELECT count(*) as num FROM res_file $con";
		$iPerPage = 18;
		$r = $DB->db_query_first($sql);
		$allNum = $r['num'];
		$pageClass = new MyPage(array (
		'total' => $allNum,
		'perpage' => $iPerPage,
		'url' => $gPageName."?act=".$gAct."&op=".$gOp."&b=".$_GET['b']."&c=".$_GET['c']."&t=".$_GET['t']."&s=".$_GET['s']."&y=".$_GET['y']."&k=".$_GET['k']
		));
		$pageClass->open_Ajax("getPage");
		$page = empty ($INCOME['page']) ? 1 : $INCOME['page'];
		$iStartNo = ($page -1) * $iPerPage;
		$sql = "SELECT * FROM res_file $con order by addTime desc LIMIT {$iStartNo}, {$iPerPage}";
		$DB->db_query($sql);
		$data = null;
		$idx = 0+($page-1)*$iPerPage;
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
			//$r['disId'] = $idx;
			if($r['ext']=="jpg" || $r['ext']=="png" || $r['ext']=="gif")
			{
				$r['pic'] = $r['path'];
			}
			else
			{
				$r['pic'] = 'images/zip.png';
			}
		    $data[] = $r;	
		}
		return array("data"=>$data,"total"=>$allNum,"pageNav"=>$pageClass->show());
	}
}
?>